
import unittest
import diabind
import testdata
import os

class SourceFileTest( unittest.TestCase ):

    def __get_srcfile(self):
        if not hasattr(self, "__srcfile"):
            glob = testdata.loaded_session.get_globalScope()
            enum = glob.findChildren( diabind.SymTagFunction,
                                      "DllMain",
                                      diabind.nsfCaseSensitive )
            self.assertEqual(len(enum), 1)

            rva = enum[0].get_relativeVirtualAddress()
            enum_lines = testdata.loaded_session.findLinesByRVA(rva, 1)
            self.assertEqual(len(enum_lines), 1)

            self.__srcfile = enum_lines[0].get_sourceFile()

        return self.__srcfile

    def test_uniqueId(self):
        srcfile = testdata.loaded_session.findFileById(self.__get_srcfile().get_uniqueId())
        self.assertEqual(srcfile.get_uniqueId(), self.__get_srcfile().get_uniqueId())

    def test_fileName(self):
        file_name = self.__get_srcfile().get_fileName()
        self.assertEqual(file_name[file_name.rfind("\\"):].lower(), "\\dllmain.cpp")

    def test_compilands(self):
        enum_compiland = self.__get_srcfile().get_compilands()
        self.assertEqual(len(enum_compiland), 1)

        lib_name = enum_compiland[0].get_libraryName()
        self.assertEqual(lib_name[lib_name.rfind("\\"):].lower(), "\\dllmain.obj")

        name = enum_compiland[0].get_name()
        self.assertEqual(name[name.rfind("\\"):].lower(), "\\dllmain.obj")

        self.assertEqual(enum_compiland[0].get_symTag(), diabind.SymTagCompiland)
